# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=sccache
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.10.0
pkgrel=1
pkgdesc='Shared compilation cache (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/mozilla/sccache"
msys2_references=(
  'archlinux: sccache'
  'purl: pkg:cargo/sccache'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl")
options=('!strip')
source=("https://github.com/mozilla/sccache/archive/v$pkgver/${_realname}-${pkgver}.tar.gz"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.10+zstd.1.5.6/download"
        "link-zstd-dynamically.patch")
sha256sums=('2c9f82c43ce6a1b1d9b34f029ce6862bedc2f01deff45cde5dffc079deeba801'
            'c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

_env() {
  export OPENSSL_NO_VENDOR=1
  export PKG_CONFIG_ALL_DYNAMIC=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
}

prepare() {
  cd "${_realname}-${pkgver}"

  patch -d ../zstd-sys-2.0.10+zstd.1.5.6 -i ../link-zstd-dynamically.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
zstd-sys = { path = "../zstd-sys-2.0.10+zstd.1.5.6" }
END

  cargo update -p zstd-sys
  cargo fetch --locked
}

build() {
  cd "${_realname}-${pkgver}"

  _env
  cargo build --release --frozen
}

check() {
  cd "${_realname}-${pkgver}"

  _env
  cargo test --release --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  _env
  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
